# Contributor: Josip <bpisoj@gmail.com>

_realname=PyICU
pkgbase=mingw-w64-python-icu
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-icu" "${MINGW_PACKAGE_PREFIX}-python3-icu")
pkgver=2.0.3
pkgrel=1
pkgdesc="Python extension wrapping the ICU C++ API (mingw-w64)"
arch=('any')
license=('BSD')
url="https://github.com/ovalhub/pyicu"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
                        "${MINGW_PACKAGE_PREFIX}-python3"
                        "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
                        "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
                        "${MINGW_PACKAGE_PREFIX}-icu")
options=('staticlibs' 'strip' '!debug')
source=("https://pypi.io/packages/source/P/${_realname}/${_realname}-${pkgver}.tar.gz"
        001-icu-config.patch)
sha256sums=('c452d14409d93819a398a93d27d5d58d6236af690d537eb2d76c8305e8d0fa5f'
            '9bca6c1cde20c5d4a7b383aead2df2cc51cbabaf2c5631c9a9de86ec37f5e64f')

prepare() {
  cd "${srcdir}"
  pushd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-icu-config.patch
  popd

  cp -r ${_realname}-${pkgver} build-${CARCH}
}

build() {
  CFLAGS+=" -fpermissive"
  export PYICU_CFLAGS="-DPYICU_VER=\"${pkgver}\""
  export PYICU_LFLAGS="-L${MINGW_PREFIX}/lib"
  export PYICU_INCLUDES="${MINGW_PREFIX}/include"
  export PYICU_LIBRARIES="icuin;icuuc;icudt"

  # Build python 2 module
  cd "${srcdir}/build-${CARCH}"
  ${MINGW_PREFIX}/bin/python2 setup.py build

  # Build python 3 module
  cd "${srcdir}/build-${CARCH}"
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package_python3-icu() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-icu")

  cd "${srcdir}/build-${CARCH}"
  ${MINGW_PREFIX}/bin/python3 setup.py install \
      --prefix=${MINGW_PREFIX#\/} \
      --root="${pkgdir}" \
      --optimize=1 \
      --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-icu/COPYING"
}

package_python2-icu() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-icu")

  cd "${srcdir}/build-${CARCH}"
  ${MINGW_PREFIX}/bin/python2 setup.py install \
      --prefix=${MINGW_PREFIX#\/} \
      --root="${pkgdir}" \
      --optimize=1 \
      --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-icu/COPYING"
}

package_mingw-w64-i686-python2-icu() {
  package_python2-icu
}

package_mingw-w64-i686-python3-icu() {
  package_python3-icu
}

package_mingw-w64-x86_64-python2-icu() {
  package_python2-icu
}

package_mingw-w64-x86_64-python3-icu() {
  package_python3-icu
}
